package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * 用户管理模块
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private JedisPool jedisPool;

    //获取用户
    @RequestMapping("/getUserByUsername")
    public Result getUserByUsername(String username){
        try {

            com.itheima.pojo.User user=userService.findUserByUsername(username);
            if (user!=null){
                return new Result(true, MessageConstant.GET_USERNAME_SUCCESS);

            }
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }


    //获取用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        try {
            System.out.println("-----------xxxxxxxxxxxxx--------------------------");
            //安全框架容器对象
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            Map<String,Object> rsp = new HashMap<>();
            /*//获取redis缓存中的 用户 - 菜单信息
            if(jedisPool.getResource().get(user.getUsername()) != null){
                rsp.put("username",user.getUsername());
                rsp.put("set",set);
            }else{

            }*/

            //添加动态菜单 ----xpl  List<Map<String,Object>>
            if(user != null){
                //获取菜单
               //List<Map<String,Object>>
                LinkedHashSet<Map<String,Object>> set = new LinkedHashSet<>();
                set =userService.getMenuByUsername(user.getUsername());
                rsp.put("username",user.getUsername());
                rsp.put("set",set);
            }
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,rsp);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }


    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){
        try {
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            userService.add(user,roleIds);
            return new Result(true,"新增用户成功.");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增用户失败!");
        }

    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = userService.findPage(queryPageBean);
            return new Result(true,"成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户分页失败!");
        }
    }


    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> role = userService.findAll();
            return new Result(true, "成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }
    }


    /**
    *@author ${xinpl}
    *@Description 根据userid 回显数据
    *@Date 2020/4/17 22:45
    *@Param [id]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/findById")
    public Result findById(int id){
        try {
            com.itheima.pojo.User user = userService.findById(id);
            return new Result(true,"数据加载成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户回显数据失败!");
        }
    }


    /**
    *@author ${xinpl}
    *@Description 根据userid 回显用户的角色id  roleIds
    *@Date 2020/4/17 22:46
    *@Param [id]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/findRoleByUserId")
    public Result findRoleByUserId(int id){
        try {
            List<Integer> roleIds = userService.findRoleByUserId(id);
            return new Result(true,"数据加载成功",roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户回显数据失败!");
        }
    }



    @RequestMapping("/update")
    public Result update(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){
        try {
            userService.update(user,roleIds);
            return new Result(true,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改用户失败!");
        }
    }

    @RequestMapping("/delete")
    public Result delete(int id){
        try {
            userService.delete(id);
            return new Result(true,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"此用户与角色关联,不能删除!");
        }
    }

    @RequestMapping("/EditPassword")
    public Result EditPassword(@RequestParam Integer pid,@RequestParam String pass){
        try {
            userService.EditPassword(pid,bCryptPasswordEncoder.encode(pass));
            return new Result(true,"重置用户密码成功.");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"重置用户密码失败!");
        }
    }


}
